package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.service.LoginServcie;
import cn.itcast.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author coolbook
 * @ClassName SendLoginCodeController
 * @Description TODO
 * @Date 19:23 2021/11/15
 * @Version 1.0
 */
@RestController
@RequestMapping("sms")
@Slf4j
@CrossOrigin
public class SendLoginCodeController {
    @Reference
    private LoginServcie loginServcie;


    /**
     * 发送登录验证码
     *
     * @param phone
     * @return
     */
    @PostMapping("sendLoginCode")
    public Result sendloginCode(String phone) {
        log.info("前端传来的电话号码" + phone);
        try {
            String flag = this.loginServcie.sendLoginCode(phone);
            if (StringUtils.isNotEmpty(flag)) {
                return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);

    }

    /**
     * 发送预约验证码
     *
     * @param phone
     * @return
     */
    @PostMapping("sendCode")
    public ResponseEntity<Result> sendCode(String phone) {
        try {
            String s = this.loginServcie.sendOrderCode(phone);
            if (StringUtils.isNotEmpty(s)) {
                return ResponseEntity.ok(new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS));
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return ResponseEntity.ok(new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL));
    }


}
